# Creating a script to generate an env.cmd file with the specified content




def generater_env_cmd(toolchain_path, sdk_path, project_path, board):
    set_path_str = f"SET PATH={toolchain_path};{toolchain_path}\\mingw64\\bin;{toolchain_path}\\bin;{toolchain_path}\\opt\\bin;{toolchain_path}\\opt\\bin\\Scripts;{toolchain_path}\\opt\\nanopb\\generator-bin;{toolchain_path}\\opt\\zephyr-sdk\\aarch64-zephyr-elf\\bin;{toolchain_path}\\opt\\zephyr-sdk\\x86_64-zephyr-elf\\bin;{toolchain_path}\\opt\\zephyr-sdk\\arm-zephyr-eabi\\bin;{toolchain_path}\\opt\\zephyr-sdk\\riscv64-zephyr-elf\\bin;%PATH%\n"
    set_pythonpath_str = f"SET PYTHONPATH={toolchain_path}\\opt\\bin;{toolchain_path}\\opt\\bin\\Lib;{toolchain_path}\\opt\\bin\\Lib\\site-packages\n"
    set_zephyr_toolchain_variant_str = "SET ZEPHYR_TOOLCHAIN_VARIANT=zephyr\n"
    set_zephyr_sdk_install_dir_str = f"SET ZEPHYR_SDK_INSTALL_DIR={toolchain_path}\\opt\\zephyr-sdk\n"
    set_zephyr_base_str = f"SET ZEPHYR_BASE={sdk_path}\\zephyr\n"

    west_build_str = f"west build --build-dir {project_path}/build {project_path} --pristine --board {board} --no-sysbuild -- -DNCS_TOOLCHAIN_VERSION=NONE -DCACHED_CONF_FILE={project_path}/prj.conf > build.log 2>&1\n"

    content = set_path_str + set_pythonpath_str + set_zephyr_toolchain_variant_str + set_zephyr_sdk_install_dir_str + set_zephyr_base_str + west_build_str

    # Writing to a file
    file_path = "env.cmd"
    with open(file_path, "w") as file:
        file.write(content)

    return file_path

def generater_build_cmd(toolchain_path, sdk_path, project_path, board):
    set_path_str = f"SET PATH={toolchain_path};{toolchain_path}\\mingw64\\bin;{toolchain_path}\\bin;{toolchain_path}\\opt\\bin;{toolchain_path}\\opt\\bin\\Scripts;{toolchain_path}\\opt\\nanopb\\generator-bin;{toolchain_path}\\opt\\zephyr-sdk\\aarch64-zephyr-elf\\bin;{toolchain_path}\\opt\\zephyr-sdk\\x86_64-zephyr-elf\\bin;{toolchain_path}\\opt\\zephyr-sdk\\arm-zephyr-eabi\\bin;{toolchain_path}\\opt\\zephyr-sdk\\riscv64-zephyr-elf\\bin;%PATH%\n"
    set_pythonpath_str = f"SET PYTHONPATH={toolchain_path}\\opt\\bin;{toolchain_path}\\opt\\bin\\Lib;{toolchain_path}\\opt\\bin\\Lib\\site-packages\n"
    set_zephyr_toolchain_variant_str = "SET ZEPHYR_TOOLCHAIN_VARIANT=zephyr\n"
    set_zephyr_sdk_install_dir_str = f"SET ZEPHYR_SDK_INSTALL_DIR={toolchain_path}\\opt\\zephyr-sdk\n"
    set_zephyr_base_str = f"SET ZEPHYR_BASE={sdk_path}\\zephyr\n"

    west_build_str = f"west build --build-dir {project_path}/build {project_path} --pristine --board {board} --no-sysbuild -- -DNCS_TOOLCHAIN_VERSION=NONE -DCACHED_CONF_FILE={project_path}/prj.conf > build.log 2>&1\n"

    content = set_path_str + set_pythonpath_str + set_zephyr_toolchain_variant_str + set_zephyr_sdk_install_dir_str + set_zephyr_base_str + west_build_str

    # Writing to a file
    file_path = "build.cmd"
    with open(file_path, "w") as file:
        file.write(content)

    return file_path

def generater_flash_cmd(toolchain_path, sdk_path, project_path, board):
    set_path_str = f"SET PATH={toolchain_path};{toolchain_path}\\mingw64\\bin;{toolchain_path}\\bin;{toolchain_path}\\opt\\bin;{toolchain_path}\\opt\\bin\\Scripts;{toolchain_path}\\opt\\nanopb\\generator-bin;{toolchain_path}\\opt\\zephyr-sdk\\aarch64-zephyr-elf\\bin;{toolchain_path}\\opt\\zephyr-sdk\\x86_64-zephyr-elf\\bin;{toolchain_path}\\opt\\zephyr-sdk\\arm-zephyr-eabi\\bin;{toolchain_path}\\opt\\zephyr-sdk\\riscv64-zephyr-elf\\bin;%PATH%\n"
    set_pythonpath_str = f"SET PYTHONPATH={toolchain_path}\\opt\\bin;{toolchain_path}\\opt\\bin\\Lib;{toolchain_path}\\opt\\bin\\Lib\\site-packages\n"
    set_zephyr_toolchain_variant_str = "SET ZEPHYR_TOOLCHAIN_VARIANT=zephyr\n"
    set_zephyr_sdk_install_dir_str = f"SET ZEPHYR_SDK_INSTALL_DIR={toolchain_path}\\opt\\zephyr-sdk\n"
    set_zephyr_base_str = f"SET ZEPHYR_BASE={sdk_path}\\zephyr\n"

    west_build_str = f"west flash -d {project_path}/build --skip-rebuild --recover > flash.log 2>&1\n"

    content = set_path_str + set_pythonpath_str + set_zephyr_toolchain_variant_str + set_zephyr_sdk_install_dir_str + set_zephyr_base_str + west_build_str

    # Writing to a file
    file_path = "flash.cmd"
    with open(file_path, "w") as file:
        file.write(content)

    return file_path

if __name__ == "__main__":
    toolchain_path = "C:\\ncs\\toolchains\\cf2149caf2"
    sdk_path = "C:\\ncs\\v2.6.0"
    project_path = "D:/Git/embuilder/debug_target"
    board = "nrf52840dk_nrf52840"

    # generater_env_cmd(toolchain_path, sdk_path, project_path, board)
    generater_build_cmd(toolchain_path, sdk_path, project_path, board)
    generater_flash_cmd(toolchain_path, sdk_path, project_path, board)